using Bag;
using Config;
using Data.Bag;
using Entity;
using Tool;
using UnityEngine;

namespace Map.MapObject.Building
{
    public class StorageBox : BaseBuilding
    {
        private StorageBoxData _storageBoxData;
        [SerializeField] private Vector2Int UIWindowGridSize;
        private int isClose;
        
        public override void Init(BaseMapBlock mapBlock, ulong id)
        {
            base.Init(mapBlock, id);
            _storageBoxData = new StorageBoxData(UIWindowGridSize.x * UIWindowGridSize.y);
            ArchiveManager.Instance.AddIMapObjectTypeData(id , _storageBoxData);
        }

        public override void OnSelect()
        {
            //打开储物箱的ui窗口
            BagManager.Instance.OnBoxShow(this, _storageBoxData.BagData, UIWindowGridSize);
        }

        public override bool DoHit(Actor actor, float dir)
        {
            if (dir >= 1 || !Input.GetMouseButtonDown(0)) return false;
            isClose++;
            if (isClose >= 2)
            {
                isClose = 0;
                return false;
            }
            Dead();
            return false;
        }

        public override void Hurt(ItemWeaponInfo weapon)
        {}

        protected override void Dead()
        {
            OnSelect();
        }

        public override bool CollisionDetectionOnPreview(LayerMask layerMask)
        {
            return BoxCollisionDetectionOnPreview(layerMask);
        }
    }
}